package cn.edu.scau.dao;

import cn.edu.scau.entity.NursingPlanShow;
import cn.edu.scau.entity.NursingRecord;
import cn.edu.scau.entity.NursingRecordShow;

import java.util.Date;
import java.util.List;

import javax.validation.constraints.NotBlank;

import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

@Mapper
@CacheNamespace
public interface NursingRecordMapper {
    @Delete({
        "delete from nursingrecord",
        "where nursingRecordID = #{nursingrecordid,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer nursingrecordid);

    @Insert({
        "insert into nursingrecord (nursingRecordID, nursingContentID, ",
        "customerID, nurseID, ",
        "nursingRecordTime)",
        "values (#{nursingrecordid,jdbcType=INTEGER}, #{nursingcontentid,jdbcType=INTEGER}, ",
        "#{customerid,jdbcType=INTEGER}, #{nurseid,jdbcType=INTEGER}, ",
        "#{nursingrecordtime,jdbcType=TIMESTAMP})"
    })
    int insert(NursingRecord record);

    @InsertProvider(type=NursingRecordSqlProvider.class, method="insertSelective")
    int insertSelective(NursingRecord record);

    @Select({
        "select",
        "nursingRecordID, nursingContentID, customerID, nurseID, nursingRecordTime",
        "from nursingrecord",
        "where nursingRecordID = #{nursingrecordid,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="nursingRecordID", property="nursingrecordid", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="nursingContentID", property="nursingcontentid", jdbcType=JdbcType.INTEGER),
        @Result(column="customerID", property="customerid", jdbcType=JdbcType.INTEGER),
        @Result(column="nurseID", property="nurseid", jdbcType=JdbcType.INTEGER),
        @Result(column="nursingRecordTime", property="nursingrecordtime", jdbcType=JdbcType.TIMESTAMP)
    })
    NursingRecord selectByPrimaryKey(Integer nursingrecordid);

	/**
	  * 返回所有的护理记录的信息
	 * @return
	 */
	@Select({ 
		"select", 
		"nursingrecordid,nursingcontentprojectname,customername,nursename,nursingrecordtime", 
		"from ((nursingrecord left join nursingcontent on nursingrecord.nursingContentID=nursingcontent.nursingContentID) left join customer on  nursingrecord.customerID=customer.customerID )left join nurse on nursingrecord.nurseID=nurse.nurseID"})
	List<NursingRecordShow> selectAll();
	
	/**
	 * 根据时间段查询
	 * @param preTime
	 * @param nextTime
	 * @return
	 */
	@SelectProvider(type = NursingRecordSqlProvider.class,method = "findByPeriod")
	List<NursingRecordShow> findByPeriod(@Param("preTime")Date preTime,@Param("nextTime")Date nextTime);
	
    @UpdateProvider(type=NursingRecordSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(NursingRecord record);

    @Update({
        "update nursingrecord",
        "set nursingContentID = #{nursingcontentid,jdbcType=INTEGER},",
          "customerID = #{customerid,jdbcType=INTEGER},",
          "nurseID = #{nurseid,jdbcType=INTEGER},",
          "nursingRecordTime = #{nursingrecordtime,jdbcType=TIMESTAMP}",
        "where nursingRecordID = #{nursingrecordid,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(NursingRecord record);
}